package com.tanhua.dubbo.api.db;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tanhua.dubbo.api.mapper.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户信息服务接口实现类
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi{

    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 保存用户信息
     * @param userInfo
     */
    @Override
    public void add(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     *更新tb_user_info表 数据
     * @param userInfo
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 根据用户id查询用户信息
     * @param currentUserId
     * @return
     */
    @Override
    public UserInfo findUserInfo(Long currentUserId) {
        return userInfoMapper.selectById(currentUserId);
    }

    /**
     * 根据黑名单用户ids查询黑名单用户信息
     *
     * @param blackUserIds
     * @return
     */
    @Override
    public List<UserInfo> findUsersByBatchIds(List<Long> blackUserIds) {
        return userInfoMapper.selectBatchIds(blackUserIds);
    }

    /**
     * 根据userIds 查询条件得到用户信息
     *
     * @param listUserIds
     * @param userInfoCondition
     * @return
     */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> listUserIds, UserInfo userInfoCondition) {
        //1 构造查询条件
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if(userInfoCondition != null) {
            Integer age = userInfoCondition.getAge();
            String gender = userInfoCondition.getGender();
            String nickname = userInfoCondition.getNickname();
            if(StringUtils.isNotBlank(nickname)){
                queryWrapper.like("nickname", nickname);
            }
            if (StringUtils.isNotBlank(gender)) {
                queryWrapper.eq("gender", gender);
            }
            if (age != null) {
                queryWrapper.lt("age", age);
            }
        }
        queryWrapper.in("id",listUserIds);
        //2 根据条件查询tb_user_info List<UserInfo>
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        //3 将List<UserInfo>转为Map<Long,UserInfo>
        Map<Long, UserInfo> userInfoMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(userInfoList)){
            /*for (UserInfo userInfo : userInfoList) {
                userInfoMap.put(userInfo.getId(),userInfo);
            }*/
            userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, b -> b));
        }
        //4 返回结果
        return userInfoMap;
    }
}
